package com.qf.dao;

import com.qf.pojo.Developer;
import com.qf.pojo.Project;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

/**
 * @ClassName ProjectDao
 * @Description TODO
 * @Author 86181
 * @Date 2022/9/7 11:55
 * @Version 1.0
 **/
public interface ProjectDao {
    //添加project
    @Insert("<script> insert into project (\n" +
            "            <trim suffixOverrides=\",\">\n" +
            "                <if test=\"pname!=null and pname!=''\">\n" +
            "                    pname,\n" +
            "                </if>\n" +
            "                <if test=\"starttime!=null\">\n" +
            "                    starttime,\n" +
            "                </if>\n" +
            "                <if test=\"endtime!=null\">\n" +
            "                    endtime,\n" +
            "                </if>\n" +
            "                <if test=\"did!=null and did!=''\">\n" +
            "                    did,\n" +
            "                </if>\n" +
            "                <if test=\"cid!=null and cid!=''\">\n" +
            "                    cid,\n" +
            "                </if>\n" +
            "            </trim>\n" +
            "        )\n" +
            "        values (\n" +
            "            <trim suffixOverrides=\",\">\n" +
            "                <if test=\"pname!=null and pname!=''\">\n" +
            "                    #{pname},\n" +
            "                </if>\n" +
            "                <if test=\"starttime!=null\">\n" +
            "                    #{starttime},\n" +
            "                </if>\n" +
            "                <if test=\"endtime!=null\">\n" +
            "                    #{endtime},\n" +
            "                </if>\n" +
            "                <if test=\"did!=null and did!=''\">\n" +
            "                    #{did},\n" +
            "                </if>\n" +
            "                <if test=\"cid!=null and cid!=''\">\n" +
            "                    #{cid},\n" +
            "                </if>\n" +
            "            </trim>\n" +
            "        )</script>")

    void addProject(Project project);

//单表全查
    @Select("select* from project where status=1")
    List<Project> findAll();
//联查
        @Results(id="projectMap",value = {
            @Result(column = "pid",property = "pid"),
            @Result(column = "pname",property = "pname"),
            @Result(column = "starttime",property = "starttime"),
            @Result(column = "endtime",property = "endtime"),
            @Result(column = "did",property = "did"),
            @Result(column = "sid",property = "sid"),
            @Result(column = "cid",property = "cid"),
            @Result(column = "updatetime",property = "updatetime"),
            @Result(column = "did",property = "developer",javaType = Developer.class,
                    one = @One(select = "com.qf.dao.DeveloperDao.findDeveloperByDid"
                            ,fetchType= FetchType.EAGER )
            ),
            @Result(column = "pid",property = "stages",
                    many = @Many(select = "com.qf.dao.StageDao.findStagesByPid",fetchType = FetchType.EAGER)
            )
    })
        @Select("select* from project where status=1")
        List<Project> findAllProject();

    //删除
    //数据库删除
//    @Delete("delete  from project where pid=#{pid}")
//    int deleteProject(int pid);
    //逻辑删除
    @Update("update project set status=0 where pid=#{pid}")
    int deleteProject(int pid);
}
